package com.robaone.test.servlets;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.robaone.html.WidgetGenerator2;

/**
 * Servlet implementation class ConvertServlet
 */
public class ConvertServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ConvertServlet() {
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}

	private void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("converter");
		if("texttojava".equals(method)){
			this.convertTextToJava(request,response);
		}else if("htmltogwt".equals(method)){
			this.convertHTMLToGWT(request,response);
		}
	}

	private void convertHTMLToGWT(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		WidgetGenerator2 converter = new WidgetGenerator2();
		String source = request.getParameter("source");
		String classname = request.getParameter("classname");
		String package_name = request.getParameter("package");
		request.setAttribute("source", this.HTMLencode(source));
		request.setAttribute("classname", classname);
		request.setAttribute("package", package_name);
		try {
			String output = converter.generateClass(source, classname, package_name);
			request.setAttribute("output", this.HTMLencode(output));
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("output", this.HTMLencode("Error: "+e.getMessage()));
			request.setAttribute("error", "yes");
		}
		request.getRequestDispatcher("/htmltogwt.jsp").forward(request, response);
	}

	private void convertTextToJava(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String source = request.getParameter("source");
		try{source = source.replaceAll("\\r", "");}catch(Exception e){}
		try{
			if(source != null && source.length() > 0){
				String[] lines = source.split("\n");
				String output = "String str = \"";
				for(int i = 0; i < lines.length;i++){
					String line = lines[i];
					if(i > 0){
						output += "\"+\n\t\"";
					}
					output += this.convert(line);
					if(i < lines.length-1){
						output += this.convert("\n");
					}
				}
				output += "\";";
				request.setAttribute("source", this.HTMLencode(source));
				request.setAttribute("output", this.HTMLencode(output));
			}
		}catch(Exception e){}
		request.getRequestDispatcher("/htmltogwt.jsp").forward(request, response);

	}

	private String HTMLencode(String source) {
		String retval = "";
		for(int i = 0; i < source.length();i++){
			if(source.charAt(i) == '<'){
				retval += "&lt;";
			}else if(source.charAt(i) == '>'){
				retval += "&gt;";
			}else if(source.charAt(i) == '&'){
				retval += "&amp;";
			}else if(source.charAt(i) == '\r'){

			}else{
				retval += source.charAt(i);
			}
		}
		return retval;
	}

	private String convert(String line) {
		String retval = "";
		for(int i = 0; i < line.length();i++){
			if(i > 0 && i % 60 == 0){
				retval += "\"+\n\t\"";
			}
			retval += this.encode(line.charAt(i));
		}
		return retval;
	}
	private String encode(char charAt) {
		switch( charAt){
		case '\n':
			return "\\n";
		case '\r':
			return "\\r";
		case '\t':
			return "\\t";
		case '"':
			return "\\\"";
		case '\\':
			return "\\\\";
		default:
			return charAt+"";
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}

}
